﻿using Autofac;
using Autofac.Integration.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Li.EntityFramework;
using Li.EntityFramework.AppService;
using Li.EntityFramework.IAppService;
using Li.EntityFramework.Repository;
using Li.EntityFramework.IRepository;

namespace Li.Web
{
    public class AutoFacConfig
    {
        public static void Register()
        {
            var builder = new ContainerBuilder();
            builder.RegisterControllers(Assembly.GetExecutingAssembly());
            #region 注入
            //builder.RegisterAssemblyTypes(Assembly.Load("Li.EntityFramework"))
            //     .Where(x => x.Name.EndsWith("Repository"))
            //     .AsImplementedInterfaces()
            //     .PropertiesAutowired()
            //     .InstancePerLifetimeScope();
            //builder.RegisterAssemblyTypes(Assembly.Load("Li.EntityFramework"))
            //    .Where(x => x.Name.EndsWith("AppService"))
            //    .AsImplementedInterfaces()
            //    .PropertiesAutowired()
            //    .InstancePerLifetimeScope();
            //builder.RegisterType<UserAppServcie>().As<IUserAppService>();
            //builder.RegisterType<UserRepository>().As<IUserRepository>();
            builder.RegisterTypes(Assembly.Load("Li.EntityFramework").GetExportedTypes()).AsImplementedInterfaces();
            #endregion
            var container = builder.Build();
            DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
        }
    }
}